# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=cfengine
# Upgrade to LTS versions only: https://cfengine.com/supported-versions/
pkgver=3.21.3
pkgrel=1
pkgdesc="A systems administration tool for networks"
url="https://www.cfengine.com/"
arch="all"
license="GPL-3.0-only"
install="$pkgname.post-install"
makedepends="$depends_dev lmdb-dev openssl-dev>3 bison flex-dev
	acl-dev pcre-dev linux-headers"
depends="procps"
subpackages="$pkgname-doc $pkgname-masterfiles::noarch"
source="https://cfengine-package-repos.s3.amazonaws.com/tarballs/cfengine-community-$pkgver.tar.gz
	https://cfengine-package-repos.s3.amazonaws.com/tarballs/cfengine-masterfiles-$pkgver.tar.gz"

_builddirmasterfiles="$srcdir"/cfengine-masterfiles-${pkgver%_p*}
options="!check" # testsuite shows some unknown errors

# secfixes:
#   3.12.2-r0:
#     - CVE-2019-9929

prepare() {
	default_prepare
	update_config_sub

	# Also extract masterfiles
	cd $srcdir
	tar xf $pkgname-masterfiles-$pkgver.tar.gz
	cd "$_builddirmasterfiles"
	update_config_guess
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--with-pic \
		--prefix=/usr \
		--enable-fhs \
		--localstatedir=/var \
		--mandir=/usr/share/man \
		--with-lmdb \
		--without-pam
	make

	cd "$_builddirmasterfiles"
	./configure \
		--prefix=/var/lib/cfengine
	make
}

package() {
	make install DESTDIR="$pkgdir"

	# not FHS but the tools seems to require those
	install -d "$pkgdir"/var/lib/cfengine/bin
	for i in "$pkgdir"/usr/bin/*; do
		ln -s ../../../../usr/bin/${i##*/} "$pkgdir"/var/lib/cfengine/bin/
	done
}

masterfiles() {
	pkgdesc="Cfengine default masterfiles"
	depends="cfengine"

	cd "$_builddirmasterfiles"
	make install DESTDIR="$subpkgdir"
}

sha512sums="
7e8f6bd669203eff285c06e15b43c27e374740b4cc797cf57f76b2c7fff990bee09d0bf8055e41b07589885ccd06492f85899febea07dabdb9a0c48de6eb5b26  cfengine-community-3.21.3.tar.gz
9d1852fdfbd2ddc196b402adf78ce97ce533bde45da649459d6216b7b542b3205f3b6d84f5f60c365f56b6d7af39ceb5965f0d7eb4ae79b6641577b0f481141f  cfengine-masterfiles-3.21.3.tar.gz
"
